<template>
  <div class="app-container">
    <el-card shadow="never">
      <div class="tip">
        <p>后台管理只能创建自营业店</p>
      </div>
      <el-form ref="storeForm" :model="form" :rules="rules" label-width="80px" class="store-creat">
        <el-form-item label="店铺名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="telephone">
          <el-input v-model="form.telephone"></el-input>
        </el-form-item>
        <el-form-item label="店铺简介">
          <el-input v-model="form.profile"></el-input>
        </el-form-item>
        <el-form-item label="店铺类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择店铺类型">
            <el-option label="自营店" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">立即创建</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { create } from '@/api/store'

export default {
  name: 'order-creat',
  data() {
    return {
      form: {
        name: '',
        address: '',
        telephone: '',
        profile: '',
        type: ''
      },
      rules: {
        name: [{ required: true, message: '请输入店铺名称', trigger: 'blur' }],
        address: [{ required: true, message: '请输入店铺详细地址', trigger: 'blur' }],
        telephone: [{ required: true, message: '请输入电话号码', trigger: 'blur' }],
        type: [{ required: true, message: '请选择店铺类型', trigger: 'blur' }]
      }
    }
  },
  created() {
  },
  methods: {
    onSubmit() {
      this.$refs['storeForm'].validate((valid) => {
        if (valid) {
          this.createStore(this.form)
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 创建店铺
    createStore(data) {
      create(data).then(res => {
        if (res.status === 'success') {
          this.$message({
            message: res.message,
            type: 'success'
          })
          this.$store.dispatch('delVisitedViews', { name: 'store' }) // 清除keep-alive
          setTimeout(() => {
            this.$router.push({ name: 'store' })
          }, 1000)
        } else {
          this.$message({
            message: res.message,
            type: 'warning'
          })
        }
      }).catch(err => {
        console.log(err)
      })
    }
  }
}
</script>

<style lang="scss">
  .store-creat {
    max-width: 600px;
  }

  .store-creat .line {
      text-align: center;
  }

  .store-creat .el-checkbox-group .el-checkbox {
    float: left;
    width: 160px;
    padding-right: 20px;
    margin: 0;
    padding: 0;
}

.app-main .tip {
    padding: 8px 16px;
    background-color: #ecf8ff;
    border-radius: 4px;
    // border-left: 5px solid #50bfff;
    margin: 20px 0;
    margin-top: 10px;
    p {
    font-size: 14px;
    color: #5e6d82;
    line-height: 1.5em;
}
}

</style>

